package org.ihelpuoo.weboo.upload.action;

import java.io.IOException;
import java.util.Calendar;

import org.apache.log4j.Logger;
import org.ihelpuoo.db.ConnectionProxy;
import org.ihelpuoo.lang.ExceptionUtils;
import org.ihelpuoo.orm.POJO;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.weboo.action.http.Action;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.upload.FileBean;

public class UpdateFileAction implements Action
{

	private static Logger	log	= Logger.getLogger(UpdateFileAction.class);

	public void execute(ActionContext context)
	{
		ConnectionProxy conn = null;
		Calendar now = Calendar.getInstance();
		try
		{
			FileBean bean = (FileBean)context.getFormObject();
			bean.setLastModifiedTime(now);
			FieldFilter actived = context.properties().getDBIOActivedFieldFilter();
			FieldFilter locked = context.properties().getDBIOLockedFieldFilter();
			context.pojo().save(bean,actived,locked);
			Segment seg = context.createPattern(context.properties().getString("return"));
			context.getResponse().getWriter().write(seg.toString());
			context.getResponse().flushBuffer();
		}
		catch (Exception e)
		{
			context.getResponse().setStatus(500);
			try
			{
				context.getResponse().getWriter().write(ExceptionUtils.dump(e));
			}
			catch (IOException e1)
			{
				log.warn(ExceptionUtils.dump(e1));
			}
		}
		finally
		{
			POJO.safeCloseConnection(conn,log);
		}
	}

}
